package com.controller;

import com.pojo.Notifications;
import com.pojo.Result;
import com.service.NotificationsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/notifications")
public class NotificationsController {

    private final NotificationsService notificationsService;

    @Autowired
    public NotificationsController(NotificationsService notificationsService) {
        this.notificationsService = notificationsService;
    }


    @PostMapping
    public ResponseEntity<Result<Void>> createNotification(@RequestBody Notifications notification) {
        Result<Void> result = notificationsService.createNotification(notification);
        return ResponseEntity.status(result.getCode()).body(result);
    }


    @PutMapping("/{id}")
    public ResponseEntity<Result<Void>> updateNotification(
            @PathVariable Integer id,
            @RequestBody Notifications notification) {
        notification.setId(id); // 确保ID一致
        Result<Void> result = notificationsService.updateNotification(notification);
        return ResponseEntity.status(result.getCode()).body(result);
    }


    @DeleteMapping("/{id}")
    public ResponseEntity<Result<Void>> deleteNotification(@PathVariable Integer id) {
        Result<Void> result = notificationsService.deleteNotification(id);
        return ResponseEntity.status(result.getCode()).body(result);
    }


    @GetMapping("/{id}")
    public ResponseEntity<Result<Notifications>> getNotificationById(@PathVariable Integer id) {
        Result<Notifications> result = notificationsService.getNotificationById(id);
        return ResponseEntity.status(result.getCode()).body(result);
    }


    @GetMapping("/user/{userId}")
    public ResponseEntity<Result<List<Notifications>>> getNotificationsByUserId(@PathVariable Integer userId) {
        Result<List<Notifications>> result = notificationsService.getNotificationsByUserId(userId);
        return ResponseEntity.status(result.getCode()).body(result);
    }


    @GetMapping("/user/{userId}/unread")
    public ResponseEntity<Result<List<Notifications>>> getUnreadNotifications(@PathVariable Integer userId) {
        Result<List<Notifications>> result = notificationsService.getUnreadNotifications(userId);
        return ResponseEntity.status(result.getCode()).body(result);
    }


    @PatchMapping("/{id}/read")
    public ResponseEntity<Result<Void>> markNotificationAsRead(@PathVariable Integer id) {
        Result<Void> result = notificationsService.markNotificationAsRead(id);
        return ResponseEntity.status(result.getCode()).body(result);
    }


    @PatchMapping("/user/{userId}/read-all")
    public ResponseEntity<Result<Void>> markAllNotificationsAsRead(@PathVariable Integer userId) {
        Result<Void> result = notificationsService.markAllNotificationsAsRead(userId);
        return ResponseEntity.status(result.getCode()).body(result);
    }
}